Imports System.Drawing
Imports System.Windows.Forms
Imports Vintasoft.Pdf.Content.TextExtraction
Imports Vintasoft.Pdf.Visualization

Namespace Pdf
	Public Partial Class FindTextForm
		Inherits Form

		#Region "Fields"

		Private _viewerTool As PdfViewerTool
		Private _stop As Boolean = False
		Private _formLocation As Point = Point.Empty
		Private _findAgain As Boolean = True

		#End Region



		#Region "Constructor"

		Public Sub New(viewerTool As PdfViewerTool)
			InitializeComponent()

			_viewerTool = viewerTool
			AddHandler _viewerTool.TextSearchingProgress, New EventHandler(Of PdfTextSearchingProgressEventArgs)(AddressOf _viewerTool_FindTextAtPage)
			lookInComboBox.SelectedIndex = 1

			_findTextOptions = New FindTextOptions()
			matchCaseCheckBox.Checked = _findTextOptions.MatchCase
			searchUpCheckBox.Checked = _findTextOptions.SearchUp
		End Sub


		#End Region



		#Region "Properties"

		''' <summary>
		''' Gets or sets a flag indicating whether search is started.
		''' </summary>
		Private Property SearchInProgress() As Boolean
			Get
				Return findWhatComboBox.Enabled
			End Get
			Set
				findWhatComboBox.Enabled = Not value
				lookInComboBox.Enabled = Not value
				findOptionsGroupBox.Enabled = Not value
				findNextButton.Visible = Not value
				stopButton.Visible = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets a text to find.
		''' </summary>
		Public Property FindWhat() As String
			Get
				Return findWhatComboBox.Text
			End Get
			Set
				If value.Contains(Environment.NewLine) Then
					value = value.Substring(0, value.IndexOf(Environment.NewLine))
				End If
				findWhatComboBox.Text = value
			End Set
		End Property

		Private _findTextOptions As FindTextOptions
		''' <summary>
		''' Gets a find text options.
		''' </summary>
		Public ReadOnly Property FindOptions() As FindTextOptions
			Get
				Return _findTextOptions
			End Get
		End Property

		Public ReadOnly Property FindIn() As PdfViewerTool.FindTextMode
			Get
				Return DirectCast(lookInComboBox.SelectedIndex, PdfViewerTool.FindTextMode)
			End Get
		End Property

		#End Region



		#Region "Methods"

		Private Sub FindTextDialog_Shown(sender As Object, e As EventArgs)
			_findAgain = True
			If Not _formLocation.IsEmpty Then
				Location = _formLocation
			End If
		End Sub

		Private Sub FindTextDialog_FormClosing(sender As Object, e As FormClosingEventArgs)
			_formLocation = Location
		End Sub

		Private Sub findWhatComboBox_TextChanged(sender As Object, e As EventArgs)
			findNextButton.Enabled = findWhatComboBox.Text <> ""
		End Sub

		Private Sub findNextButton_Click(sender As Object, e As EventArgs)
			If _viewerTool.ImageViewer.Image Is Nothing Then
				Return
			End If

			Dim text As String = findWhatComboBox.Text

			_findTextOptions.MatchCase = matchCaseCheckBox.Checked
			_findTextOptions.SearchUp = searchUpCheckBox.Checked

			If findWhatComboBox.Items.Contains(text) Then
				findWhatComboBox.Items.Remove(text)
			End If
			findWhatComboBox.Items.Insert(0, text)

			Dim findMode As PdfViewerTool.FindTextMode = FindIn
			If findMode = PdfViewerTool.FindTextMode.AllPages Then
				SearchInProgress = True
			End If
			_stop = False

			If _findAgain Then
				_viewerTool.FindText(text, findMode, _findTextOptions)
			Else
				_viewerTool.FindTextNext(text, findMode, _findTextOptions)
			End If
			_findAgain = False

			If findMode = PdfViewerTool.FindTextMode.AllPages Then
				SearchInProgress = False
			End If
		End Sub

		Private Sub _viewerTool_FindTextAtPage(sender As Object, e As PdfTextSearchingProgressEventArgs)
			If _stop Then
				e.Cancel = True
			End If
		End Sub

		Private Sub stopButton_Click(sender As Object, e As EventArgs)
			_stop = True
		End Sub

		Private Sub FindTextDialog_KeyDown(sender As Object, e As KeyEventArgs)
			If e.KeyCode = Keys.Escape Then
				DialogResult = DialogResult.Cancel
			End If
		End Sub


		Private Sub matchCaseCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			_findTextOptions.MatchCase = matchCaseCheckBox.Checked
		End Sub

		Private Sub searchUpCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			_findTextOptions.SearchUp = searchUpCheckBox.Checked
		End Sub

		#End Region

	End Class
End Namespace
